package cn.hchaojie.web;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DemoCookie extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		Date d = new Date();
		
		DateFormat formmater = new SimpleDateFormat("yyyy-MM-dd/HH:mm:ss");
		
		//  访问时间
		String date = formmater.format(d);
		
		// Cookie[] cookies = req.getCookies();
		// cookie
		Cookie cookie = new Cookie("accessDate", date);
		
		// 只要过期时间没到，cookie就一直存在文件里面，浏览器关闭也不影响
		cookie.setMaxAge(60);	// expire
		response.addCookie(cookie);
		
		// 和response.addCookie效果一样
		// response.addHeader("Set-Cookie", date);
		
		// 让cookie持久化，存到文件里面，浏览器关闭之后，下次启动仍然能够访问
		
	}
}
